package com.winning.pmph.ctrl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winning.pmph.entity.Role;
import com.winning.pmph.entity.User;
import com.winning.pmph.service.DataPermissionService;
import com.winning.pmph.service.RoleService;
import com.winning.pmph.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(value = "系统管理/用户管理")
@RestController
@RequestMapping("/pmph/action/UserCtrl")
public class UserCtrl {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @Resource
    private DataPermissionService bookPermissionService;

    @ApiOperation(value = "selectUserListWithRoleNames", notes = "获取角色")
    @GetMapping("selectUserListWithRoleNames")
    public List<User> selectUserListWithRoleNames() {
        List<User> userList = userService.list();
        Map<String, Role> roleMap = roleService.list().stream().collect(Collectors.toMap(Role::getId, item -> item));
        for (User user : userList) {
            String roleNames = user.getRoleIds().stream().filter(roleMap::containsKey).map(roleMap::get).map(Role::getRoleName).collect(Collectors.joining(" "));
            user.setRoleNames(roleNames);
        }
        return userList;
    }

    @ApiOperation(value = "selectRoleName", notes = "获取角色名称")
    @GetMapping("selectRoleName")
    public List<Map<String, Object>> selectRoleName() {
        List<Role> roles = roleService.lambdaQuery().eq(Role::getStatus, "true").list();
        List<Map<String, Object>> list = new ArrayList<>();
        for (Role role : roles) {
            Map<String, Object> map = new HashMap<>();
            map.put("label", role.getRoleName());
            map.put("value", role.getId());
            list.add(map);
        }
        return list;
    }

    @ApiOperation(value = "selectUserList", notes = "获取用户列表接口")
    @GetMapping("selectUserList")
    public Page<User> selectUserList(String userName,
                                           String name,
                                           String system,
                                           Integer currentPage,
                                           Integer pageSize) {
        Page<User> page = userService.lambdaQuery()
                .like(StringUtils.isNotBlank(userName), User::getUserName, userName)
                .eq(StringUtils.isNotBlank(name), User::getName, name)
                .eq(StringUtils.equals(system, "inside"), User::getKind, 0)
                .eq(StringUtils.equals(system, "outside"), User::getKind, 1)
                .orderByDesc(User::getUpdatedTime)
                .orderByAsc(User::getId)
                .page(new Page<>(currentPage, pageSize));

        Map<String, String> id2NameMap = roleService.list().stream().collect(Collectors.toMap(Role::getId, Role::getRoleName));
        for (User user : page.getRecords()) {
            String roleName = user.getRoleIds().stream()
                    .map(id2NameMap::get)
                    .filter(StringUtils::isNotEmpty).collect(Collectors.joining(","));
            user.setRoleNames(roleName);
        }
        return page;
    }

    @ApiOperation(value = "newUser", notes = "添加或编辑用户接口")
    @PostMapping("newUser")
    public void newUser(@RequestBody User user) {
        userService.saveUser(user);
    }

    @ApiOperation(value = "selectUserName", notes = "获取用户名称")
    @GetMapping("selectUserName")
    public List<Map<String, String>> selectUserName() {
        List<User> list = userService.list();
        List<Map<String, String>> userList = new ArrayList<>();
        for (User user : list) {
            Map<String, String> map = new HashMap<>();
            map.put("label", user.getName());
            map.put("value", user.getUserName());
            userList.add(map);
        }
        return userList;
    }

    @ApiOperation(value = "updateUser", notes = "修改用户接口")
    @PostMapping("updateUser")
    public void updateUser(String user) {
        User users = JSON.parseObject(user, User.class);
        if (users.getStatus() == User.BLOCK) {
            users.setStatus(User.ENABLE);
        } else {
            users.setStatus(User.BLOCK);
        }
        userService.saveOrUpdate(users);
    }
}